// pages/profile/index.ts
Page({

  data: {
    userInfo: {
      nickName: '',
      avatar: ''
    }
  },
  onLoad() {
    this.setData({ userInfo: wx.getStorageSync('userInfo') })
  },
  getUserNickName(ev) {
    this.updateNickName(ev.detail.value)
  },

  async updateNickName(nickName) {
    const { code } = await wx.http.put('/userInfo', { nickName })
    if(code !== 10000 ) return wx.utils.toast()

    this.setData({ 'userInfo.nickName': nickName })

  },

  getUserAvatar(ev) {
    this.updateUserAvatar(ev.detail.avatarUrl)
  },
  updateUserAvatar(avatarURL) {
    console.log('aaa',getApp().token);
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: avatarURL,
      name: 'file',
      header: {
        Authorization: getApp().token
      },
      formData: {
        type: 'avatar'
      },
      success: (res) => {
        console.log(res);
        const data = JSON.parse(res.data)
        console.log(data);

        if(data.code !== 10000) return wx.utils.toast()

        this.setData({ 'userInfo.avatar':data.data.url })
      }
    })
  },
  

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})